﻿$(function () {
    show();
})
function show() {
    layui.use(function () {
        var table = layui.table;
    var deliveryNumber = $("#deliveryNumber").val();
    var finishedProductNumber = $("#finishedProductNumber").val();
    var state = $("#state").val();
    // 先进行表格的初始渲染
    table.render({
        elem: '#DeliveryTable',  // 表格元素的选择器
        page: {
            limit: 5,
            limits: [5, 8, 10]
        },
        toolbar: '#toolbarDemo',
        defaultToolbar: ['exports'],
        cols: [[
            { type: 'checkbox', fixed: 'left', title: '复选框' },
            { field: 'deliveryNumber', title: '出库编号' },
            { field: 'deliveryDate', title: '出库日期' },
            { field: 'orderNumber', title: '订单编号' },
            { field: 'finishedProductNumber', title: '成品编号' },
            {
                field: 'state', title: '状态', templet: function (d) {
                    return PriorityState(d.state);
                }
            },
            {
                field: '', title: '操作', templet: function (d) {
                    return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.deliveryId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' + '<a class="layui-btn layui-btn-danger layui-btn-xs"  onclick="chuku(' + d.deliveryId + ')"><i class="layui-icon layui-icon-delete"></i>出库</a>' +
                        '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.deliveryId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>' + '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="UpdateData(' + d.deliveryId + ')"><i class="layui-icon layui-icon-delete"></i>修改</a>';
                }

            }
        ]],
        // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
        data: []
    });
    $.ajax({
        url: "/Delivery/DeliveryShow",
        type: "get",
        data: {
            deliveryNumber: deliveryNumber,
            finishedProductNumber: finishedProductNumber,
            state: state,
            token: localStorage.getItem("token")
        },
        success: function (data) {

            data = JSON.parse(data);
            console.log(data.data)
            // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
            table.reload('DeliveryTable', {
                page: {
                    limit: 5,
                    limits: [5, 8, 10]
                },
                data: data.data
            });
        },
        error: function () {
            console.log("请求失败");
        }
    });
});
}
window.editData = function (id) {
    localStorage.setItem("deliveryId", id)
    console.log(id)
    window.location.href = "/Delivery/DeliveryModelView";
}
window.UpdateData = function (id) {
    localStorage.setItem("deliveryId", id)
    console.log(id)
    window.location.href = "/Delivery/DeliveryUpdateView";
}
window.deleteData = function (id) {
    console.log(id)
    $.ajax({
        url: '/Delivery/DeliveryDelete',
        type: 'post',
        data: { data: JSON.stringify({ deliveryId: id }), token: localStorage.getItem("token") },
        success: function (res) {
            var s = JSON.parse(res)
            console.log(res)
            if (s.data >= 1) {
                layer.alert("删除成功");
                setTimeout(function () {
                    location.reload();
                }, 2000);

            }
            else {
                layer.alert("删除失败");
            }
        }
    })
}
window.chuku = function (id) {
    console.log(id)
    $.ajax({
        url: '/Delivery/DeliveryUpdateState',
        type: 'post',
        data: { data: JSON.stringify({ deliveryId: id }), token: localStorage.getItem("token") },
        success: function (res) {
            var s = JSON.parse(res)
            console.log(res)
            if (s.data >= 1) {
                layer.alert("出库成功");
                setTimeout(function () {
                    location.reload();
                }, 2000);

            }
            else {
                layer.alert("出库失败");
            }
        }
    })
}
    
    
    


    


function formatTime(timeStamp) {
    if (timeStamp) {
        var date = new Date(timeStamp);
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var day = ('0' + date.getDate()).slice(-2);
        var hour = ('0' + date.getHours()).slice(-2);
        var minute = ('0' + date.getMinutes()).slice(-2);
        var second = ('0' + date.getSeconds()).slice(-2);
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    return "";
}
function PriorityState(state) {
    debugger
    if (state == 2) {
        return "已出库"
    }
    else {
        return "未出库"
    }
}
function Add() {
    debugger;
    window.location.href = "/Delivery/DeliveryAddView";
};

